import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ThemeConfig with ChangeNotifier {
  ThemeConfig() {
    _loadSavedTheme();
  }
  // 默认主题
  static LinearGradient get defaultGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFDBEAFE), Color.fromARGB(255, 255, 255, 255), Color(0xFFFFFFFF)],    
      );

  //白色主题
  static LinearGradient get whiteGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFFFFFFF), Color(0xFFFFFFFF)],
      );

  // 粉色主题
  static LinearGradient get pinkGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFFDF2F8), Color(0xFFFCE7F3)],
      );

  // 绿色主题
  static LinearGradient get greenGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFF0FDF4), Color(0xFFDCFCE7)],
      );

  // 橙色主题
  static LinearGradient get orangeGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFFFF7ED), Color(0xFFFFEDD5)],
      );

  // 浅蓝色渐变
  static LinearGradient get skyGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFEFF6FF), Color(0xFFF8FAFC)],
      );

  //太阳照射大地色彩
  static LinearGradient get sunGradient => const LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color.fromARGB(255, 247, 211, 12),Color.fromARGB(255, 255, 255, 255),Color.fromARGB(255, 255, 255, 255), Color.fromARGB(131, 59, 54, 23)],
      );
  // 默认文字颜色
  static Color get defaultTextColor => Colors.black;

  // 当前主题
  LinearGradient currentGradient = defaultGradient;

  // 当前文字颜色
  Color currentTextColor = defaultTextColor;

  // 当前主题主色（用于强调色/按钮/指示器等）
  Color currentPrimaryColor = Colors.blue;

  // 主题列表
  static final List<Map<String, dynamic>> themes = [
    {
      'name': '默认主题',
      'gradient': defaultGradient,
      'primaryColor': Colors.blue,
      'textColor': defaultTextColor,
    },
    {
      'name': '白色主题',
      'gradient': whiteGradient,
      'primaryColor': Colors.blue,
      'textColor': defaultTextColor,
    },
    {
      'name': '粉色主题',
      'gradient': pinkGradient,
      'primaryColor': Colors.pink,
      'textColor': defaultTextColor,
    },
    {
      'name': '绿色主题',
      'gradient': greenGradient,
      'primaryColor': Colors.green,
      'textColor': defaultTextColor,
    },
    {
      'name': '橙色主题',
      'gradient': orangeGradient,
      'primaryColor': Colors.orange,
      'textColor': defaultTextColor,
    },
    {
      'name': '浅蓝色渐变',
      'gradient': skyGradient,
      'primaryColor': Colors.lightBlue,
      'textColor': defaultTextColor,
    },
    {
      'name': '阳光主题',
      'gradient': sunGradient,
      'primaryColor': Colors.black,
      'textColor': defaultTextColor,
    },
  ];

  // 切换主题
  void changeTheme(int index) {
    if (index >= 0 && index < themes.length) {
      currentGradient = themes[index]['gradient'];
      currentTextColor = themes[index]['textColor'];
      currentPrimaryColor = themes[index]['primaryColor'] ?? Colors.blue;
      notifyListeners();
      _saveThemeIndex(index);
    }
  }

  // 获取当前主题索引
  int get currentThemeIndex {
    for (int i = 0; i < themes.length; i++) {
      if (themes[i]['gradient'] == currentGradient) {
        return i;
      }
    }
    return 0;
  }

  Future<void> _loadSavedTheme() async {
    final prefs = await SharedPreferences.getInstance();
    final index = prefs.getInt('theme_index');
    if (index != null && index >= 0 && index < themes.length) {
      currentGradient = themes[index]['gradient'];
      currentTextColor = themes[index]['textColor'];
      currentPrimaryColor = themes[index]['primaryColor'] ?? Colors.blue;
      notifyListeners();
    }
  }

  Future<void> _saveThemeIndex(int index) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt('theme_index', index);
  }
}
